// 拼后台传过来的listjson并插入到表格中
function updateList(data){
    var dataL = data.length;
    var html = "";
    for(var i = 0; i < dataL; i++){
        html = html + "<tr>"
            +  "<td>" + zhigong.undefinedHandler(data[i].id) + "</td>"
            +  "<td>" + zhigong.undefinedHandler(data[i].name) + "</td>"
            +  "<td>"
            +  "<a href=\"javascript:openModal('view'," + data[i].id  +")\" class=\"btn btn-primary btn-xs btn-margin\" >查看</a>"
            +  "<a href=\"javascript:openModal('update'," + data[i].id + ")\" class=\"btn btn-info btn-xs btn-margin\">修改</a>"
            +  "<a href=\"javascript:zhigong.deleteData('role', " + data[i].id + ")\"  data-toggle=\"confirmation\" data-placement=\"bottom\" class=\"btn btn-danger btn-xs btn-margin\">删除</a>"
            +  "</td>"
            +"</tr>";
    }

    var tbodyList = $("#tbodyList");
    tbodyList.html(html);
}

function toMenuList(){
    if($("#selectedMenu").val()) {
        var value = $("#selectedMenu").val();
        var text = $("option[value='" + value + "']").text();
        var optionHtml = $("#menuList").html() + "<option value=" + value + ">" + text + "</option>";
        $("#menuList").html(optionHtml);
        $("#selectedMenu option[value='" + value + "']").remove();
    }
    return false;
}
function toselectedMenu(){
    if($("#menuList").val()){
        var value=$("#menuList").val();
        var text=$("option[value='"+value+"']").text();
        var optionHtml = $("#selectedMenu").html()+"<option value="+value+">"+text+"</option>";
        $("#selectedMenu").html(optionHtml);
        $("#menuList option[value='"+value+"']").remove();
    }
    return false;
}

function saveRoleData(){
    var options = $("#selectedMenu option");
    var menuList = [];
    for(var i = 0 ; i < options.length; i++){
        menuList[i] = {
            id : options.eq(i).val(),
            name : options.eq(i).text()
        }
    }
    // 获取上报数据,json字符串格式.
    var data = {
        name : $("#name").val(),
        menuList : menuList
    };

    // 默认执行新增的保存
    var status = "POST";
    var ajaxUrl = '/roles';
    // 取主键input,看是否有值,如果有值认为是更新的保存
    var idVal = $("#id").val();
    // idVal如果不为空则为更新
    if(idVal!='' && idVal!= null) {
        data.id = idVal;
        status = "PUT";
        ajaxUrl = '/roles/'+idVal;
    }
    $.ajax({
        url : ajaxUrl,
        type : status,
        dataType:"json",
        contentType : "application/json;charset=UTF-8",
        data : JSON.stringify(data),
        success:function(result) {
            var message= JSON.stringify(result);
            console.log("success:"+message);

            // 显示保存成功
            $("#alertMsg").html(result.data == 1 ? "保存成功" : "保存失败").addClass('alert-success').addClass('in');

            // 关闭模态框
            $('#dataModal').modal('hide');

            // 一秒后刷新页面
            setTimeout(function(){
                $("#alertMsg").removeClass('in');
                window.location.reload();
            },1000);
        },
        error:function(result){
            var message= JSON.stringify(result);
            console.log("error:"+message);
            $("#alertMsg").html(result.error).addClass('alert-danger').addClass('in');
        }
    });
}

// 角色单独处理打开模态框方法
// module: 模块名;
// status: 状态值,new新增,view查看,update修改;
// prikey: 主键,可为数组
function openModal ( status, prikey ){
    if(status == 'new'){
        initMenuList();
        $('#dataModal').modal('show');
    }else{
        // 获取角色数据
        $.ajax({
            url : zhigong.URL.urlWithParam('role',prikey),
            type : "GET",
            dataType:"json",
            contentType : "application/json;charset=UTF-8",
            success:function(result) {
                <!-- 处理后端返回的数据 -->
                var message= JSON.stringify(result);
                console.log("role info:"+message);
                var data = result.data;
                for(var d in data){
                    if(d!= 'menuList') $("#"+d).val(data[d]);
                }
                var selectedMenuList = data.menuList;
                var dataL = selectedMenuList.length;
                var html = "";
                for(var i = 0; i < dataL; i++){
                    html = html + "<option value="+ selectedMenuList[i].id +">"+ selectedMenuList[i].name +"</option>>";
                }
                $("#selectedMenu").html(html);

                if(status == 'view'){
                    $('#dataModal').find('.form-control').attr("disabled","disabled");
                    $('#saveBtn').hide();
                }
                $('#dataModal').modal('show');
            },
            error:function(result){
                var message= JSON.stringify(result);
                console.log("error:"+message);
            }
        });


        // 获取当前角色未选择的菜单列表
        $.ajax({
            url : '/roles/unselectedmenus/'+prikey,
            type : "GET",
            dataType:"json",
            contentType : "application/json;charset=UTF-8",
            success:function(result) {
                <!-- 处理后端返回的数据 -->
                var message= JSON.stringify(result);
                console.log("unselectedMenu:"+message);

                var resData = result.data;
                var dataL = resData.length;
                var html = "";
                for(var i = 0; i < dataL; i++){
                    html = html + "<option value="+ resData[i].id +">"+ resData[i].name +"</option>>";
                }
                $("#menuList").html(html);
            },
            error:function(result){
                var message= JSON.stringify(result);
                console.log("error:"+message);
            }
        });

    }
}

// 初始化返回菜单列表
function initMenuList(){
    // 返回菜单列表
    $.ajax({
        url: zhigong.URL.urlNoParam('menu'),
        type: "GET",
        dataType: "json",
        data: {
            pageNumber : -1
        },
        contentType: "application/json;charset=UTF-8",
        success: function (result) {

            var message= JSON.stringify(result);
            console.log("init unselectedMenu:"+message);

            var resData = result.data;
            var dataL = resData.length;
            var html = "";
            for(var i = 0; i < dataL; i++){
                html = html + "<option value="+ resData[i].id +">"+ resData[i].name +"</option>>";
            }
            $("#menuList").html(html);
            $("#selectedMenu").html("");

        },
        error: function (result) {
            var message = JSON.stringify(result);
            console.log("error:" + message);
        }
    })
}


$(function(){

    initMenuList();

    // 初始化加载列表数据
    zhigong.pageData("role", 1, true);


})

